package org.kymjs.kjframe.demo;

import static org.kymjs.kjframe.common.Constant.HEADER_DEVICE_KEY;
import static org.kymjs.kjframe.common.Constant.WS_BASE;
import static org.kymjs.kjframe.common.Constant.WS_HEART_BASE;
import static org.kymjs.kjframe.ui.ViewInject.toast;

import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.json.JSONException;
import org.json.JSONObject;
import org.kymjs.kjframe.utils.WebsocketClient;

import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;

public class HeartClient {
    private WebsocketClient wsClient;
    private boolean wsConnected = false;
    private HeartClient.InviteCodeInterface inviteCodeInterface;
    private String wsUrl;

    public HeartClient(HeartClient.InviteCodeInterface inviteCodeInterface){
        wsUrl = WS_HEART_BASE;
        this.inviteCodeInterface = inviteCodeInterface;
    }

    public Boolean start(){
        if (wsClient == null || !wsConnected) {
            initWebSocket();
            return true;
        } else {
            return false;
        }
    }
    public Boolean getConnectState(){
        return wsClient != null && wsConnected;
    }
    public void stop(){
        wsClient.stop();
    }

    private void initWebSocket() {
        wsClient = new WebsocketClient(wsUrl, new WebSocketListener() {
            @Override
            public void onClosed(@NonNull WebSocket webSocket, int code, @NonNull String reason) {
                super.onClosed(webSocket, code, reason);
                wsConnected = false;
                System.out.println("关闭心跳ws连接");
            }

            @Override
            public void onFailure(@NonNull WebSocket webSocket, @NonNull Throwable t, @Nullable Response response) {
                super.onFailure(webSocket, t, response);
                wsConnected = false;
                System.out.println("心跳ws连接失败");
                System.out.println("失败原因：" + response);
            }

            @Override
            public void onOpen(@NonNull WebSocket webSocket, @NonNull Response response) {
                super.onOpen(webSocket, response);
                wsConnected = true;
                System.out.println("心跳ws连接开启");
//                检查投屏码是否可以激活
                inviteCodeInterface.onConnected();
            }

            @Override
            public void onMessage(@NonNull WebSocket webSocket, @NonNull String text) {
                super.onMessage(webSocket, text);
                System.out.println("心跳ws接收到了消息");
                System.out.println(text);
                handleRequest(text);
            }
        });
    }
    private void handleRequest(String message) {
        if (message.equals("投屏码激活成功")||message.contains("投屏码已使用")){
            inviteCodeInterface.onEditInviteCode();
        }
        new Handler(Looper.getMainLooper()).postDelayed(() -> {
            toast(message);
        }, 1000); // 延迟 3 秒
    }

    public void send(String text){
//        System.out.println("发送text"+text);
        wsClient.sendMessage(text);
    }

    public interface InviteCodeInterface {
        void onConnected();
        void onEditInviteCode();
    }
    }
